import ViewOrientation from "./ViewOrientation";
import Watcher from "./Watcher";

const { ccclass, property } = cc._decorator;

@ccclass
export default class BgAdaptive extends cc.Component {

    start() {

        Watcher.on(ViewOrientation.ON_VIEW_RESIZE, this,this.resize);
        this.resize();
    }

    resize() {

        let srcSize = this.node.getContentSize();
        let scale = srcSize.width / srcSize.height;
        let viewSize = cc.view.getVisibleSize();

        if(viewSize.height > viewSize.width){

            cc.Canvas.instance.fitHeight = true;
            cc.Canvas.instance.fitWidth = false;
        }
        else
        {
            cc.Canvas.instance.fitHeight = false;
            cc.Canvas.instance.fitWidth = true;
        }
        
        if (srcSize.height < viewSize.height) {
            srcSize.height = viewSize.height;
            srcSize.width = srcSize.height * scale;
        }
        this.node.setContentSize(srcSize);
    }


}
